package com.sunseagear.wind.modules.task.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sunseagear.common.base.http.R;
import com.sunseagear.common.base.http.RPage;
import com.sunseagear.common.base.mvc.controller.BaseBeanController;
import com.sunseagear.common.utils.ObjectUtils;
import com.sunseagear.common.utils.StringUtils;
import com.sunseagear.wind.common.aspectj.annotation.Log;
import com.sunseagear.wind.common.aspectj.enums.LogType;
import com.sunseagear.wind.modules.task.entity.ScheduleJobLog;
import com.sunseagear.wind.modules.task.entity.query.ScheduleJobLogQuery;
import com.sunseagear.wind.modules.task.service.IScheduleJobLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 *
 * @version V1.0
 * @package com.sunseagear.wind.modules.task.controller
 * @title: 任务日志控制器
 * @description: 任务日志控制器 * @date: 2018-09-17 14:25:19
 * @copyright: 2018 www.sunseagear.com Inc. All rights reserved.
 */

@RestController
@RequestMapping("/task/schedule/job/log")
@Log(title = "计划任务日志")
@RequiredArgsConstructor
public class ScheduleJobLogController extends BaseBeanController {

    private final IScheduleJobLogService scheduleJobLogService;

    @GetMapping(value = "list")
    @Log(logType = LogType.SELECT)
    @SaCheckPermission("task:schedule:joblog:list")
    public RPage<ScheduleJobLog> list(ScheduleJobLogQuery query) {
        //加入条件
        LambdaQueryWrapper<ScheduleJobLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(ScheduleJobLog::getCreateDate);
        queryWrapper.like(ObjectUtils.isNotEmpty(query.getJobName()),ScheduleJobLog::getJobName,query.getJobName());
        queryWrapper.eq(ObjectUtils.isNotEmpty(query.getExecuteClass()),ScheduleJobLog::getExecuteClass, query.getExecuteClass());
        queryWrapper.eq(ObjectUtils.isNotEmpty(query.getStatus()),ScheduleJobLog::getStatus, query.getStatus());
        // 预处理
        Page<ScheduleJobLog> pageBean = scheduleJobLogService.page(getPage(), queryWrapper);
        return RPage.success(pageBean);
    }

    /**
     * 删除
     * @param idList 主键集合
     * @return 操作结果
     */
    @DeleteMapping("delete")
    @Log(logType = LogType.DELETE)
    @SaCheckPermission("task:schedule:joblog:delete")
    public R<Void> delete(@RequestBody List<Long> idList) {
        scheduleJobLogService.removeBatchByIds(idList);
        return R.success("删除成功");
    }
}
